package com.walker.service.common;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import com.walker.util.CommonDao;
import com.walker.util.CommonUtils;
import com.walker.util.DateUtils;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@Service("commonService")
public class CommonService {

	@Autowired
	@Qualifier("dmJdbcTemplate")
	private JdbcTemplate dmJdbcTemplate;

	@Autowired
	private CommonDao commonDao;
	
	/**
	 *插入注册表 
	 * @param object_type
	 * @param object_id
	 * @param object_code
	 */
	public void insertObjectRegister(String object_type, String object_id, String object_code) {
		Map<String, Object> registParams = new HashMap<String, Object>();
		String id = UUID.randomUUID().toString().replaceAll("-", "");
		registParams.put("id", id);
		registParams.put("object_type", object_type);
		registParams.put("object_id", object_id);
		registParams.put("object_code", object_code);
		registParams.put("status", "0");
		registParams.put("oper_time", DateUtils.dateToString(new Date(), DateUtils.DATE_TIME_FORMAT));
		// 插入注册表
		StringBuilder sqlSb = new StringBuilder("insert into t_meta_object_register (");
		commonDao.insertTableData(registParams, sqlSb);

	}

	/**
	 * 获取字典表信息
	 * @return
	 */
	public String getDictParams() {
		JSONObject obj = new JSONObject();

		String sql = "select param_key,group_concat(param_value) as param_values,group_concat(param_name) as param_names from t_meta_public_param group by param_key";
		List<Map<String, Object>> dicts = dmJdbcTemplate.queryForList(sql);

		for (Map<String, Object> dict : dicts) {

			JSONArray ja = new JSONArray();

			String[] pvs = dict.get("param_values").toString().split(",");
			String[] pns = dict.get("param_names").toString().split(",");

			if ("engine_type".equals(dict.get("param_key"))) {
				for (int i = 0; i < pvs.length; i++) {
					JSONObject json = new JSONObject();
					json.put("key", Integer.parseInt(pvs[i]));
					json.put("value", pns[i]);

					ja.add(json);
				}
			} else {
				for (int i = 0; i < pvs.length; i++) {
					JSONObject json = new JSONObject();
					json.put("key", pvs[i]);
					json.put("value", pns[i]);

					ja.add(json);
				}
			}

			obj.put(dict.get("param_key"), ja);
		}
		return obj.toString();
	}

	/**
	 * 查询分区信息
	 * @param busin_id
	 * @return
	 */
	public Map<String, Object> getDsInfo(Object busin_id) {
		String busSql = "select param_key,param_value from t_meta_business_field_param where busin_id = ?";
		List<Map<String, Object>> dsList = dmJdbcTemplate.queryForList(busSql, busin_id);
		Map<String, Object> dsMap = new HashMap<String, Object>();
		for (Map<String, Object> ds : dsList) {
			dsMap.put(CommonUtils.valueOf(ds.get("param_key")), ds.get("param_value"));
		}
		return dsMap;
	}
}